package cn.charmaine.org.controller;

import cn.charmaine.basic.annotation.PreAuthorize;
import cn.charmaine.basic.util.AjaxResult;
import cn.charmaine.basic.util.PageList;
import cn.charmaine.org.domain.Department;
import cn.charmaine.org.query.DepartmentQuery;
import cn.charmaine.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门后端接口
 */
@RestController //@Controller+@ResponseBody
@RequestMapping("/dept")
@Api(value = "部门的API",description="部门相关的CRUD功能") //接口文档的注解
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    /**
     * 查询所有
     *
     * @GetMapping 请求方式为get http://localhost:8080/dept
     */
    @GetMapping
    @ApiOperation(value = "查询所有" )
    public List<Department> queryAll() {
        return departmentService.queryAll();
    }

    /**
     * 根据id查询
     *
     * @GetMapping 请求方式为get http://localhost:8080/dept/1
     * @ApiParam(name="编号",required = true) 为true必须传值
     */
    //@RequestMapping(value = "/{id}",method = RequestMethod.GET)
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public Department queryById( @ApiParam(name="部门id",required = true) @PathVariable("id") Long id) {
        return departmentService.queryById(id);
    }

    /**
     * 分页查询
     *
     * @PostMapping 请求方式为post http://localhost:8080/dept
     * @RequestBody 请求体  接收客户端发送的json格式数据
     */
    @PostMapping
    @ApiOperation(value = "分页查询" )
//    @PreAuthorize(name = "部门列表",value = "dept:list")
    public PageList<Department> queryByPage(@RequestBody DepartmentQuery query) {
        return departmentService.queryData(query);
    }

    /**
     * 添加或者修改
     *
     * @PutMapping 请求方式为put http://localhost:8080/dept
     * @RequestBody 请求体  接收客户端发送的json格式数据
     */
    @PutMapping
    @ApiOperation(value = "添加或者修改" )
    public AjaxResult saveOrUpdate(@RequestBody Department department) {
        try {
            if (department.getId() == null) {
                departmentService.add(department);
            } else {
                departmentService.update(department);
            }
            return new AjaxResult();//用无参的表示成功
        } catch (Exception e) {
            e.printStackTrace();
            /*return new AjaxResult(false, "操作失败");*/
            return AjaxResult.thisSuccess().setSuccess(false).setMessage("操作失败");
        }
    }

    /**
     * 根据id删除
     *
     * @DeleteMapping 请求方式为delete http://localhost:8080/dept/1
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过ID删除" )
    @PreAuthorize(name = "删除部门",value = "dept:deleteById")
    public AjaxResult deleteById(@PathVariable("id") Long id) {
        try {
            departmentService.deleteById(id);
            return new AjaxResult();//用无参的表示成功
        } catch (Exception e) {
            e.printStackTrace();
           /* return new AjaxResult(false, "操作失败");*/
            return AjaxResult.thisSuccess().setSuccess(false).setMessage("操作失败");
        }
    }

    /**
     * 批量删除
     */
    @PatchMapping
    @ApiOperation(value = "批量删除" )
    public AjaxResult batchRemove(@RequestBody List<Long> ids){
        try{
            departmentService.batchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            /*return new AjaxResult(false,"操作失败！");*/
            return AjaxResult.thisSuccess().setSuccess(false).setMessage("操作失败");
        }
    }


    /**
     * 查询部门无限极树
     *
     * @return
     */
    @GetMapping("/tree")
    public List<Department> queryTree(){
        return departmentService.queryTree();
    }
}









